package com.net
{
	import flash.events.*;
	import flash.net.*;
	
	public class HTTP extends Object
	{
		private var _loader:URLLoader;
		private var _request:URLRequest;
		public var url:String;
		public var onComplete:Function;
		public var onError:Function;
		
		/**指定以文本形式接收已下载的数据*/
		public static const TEXT:String = "text";
		/**指定以原始二进制数据形式接收下载的数据*/
		public static const BINARY:String = "binary";
		/**指定以 URL 编码变量形式接收下载的数据。*/
		public static const VARIABLES:String = "variables";
		
		/**上传xml*/
		public static const XML:String = "text/xml";
		//		/**上传text*/
		//		public static const UP_TEXT:String = "text/plain";
		//		/**上传二进制文件*/
		//		public static const UP_BINARY:String = "application/octet-stream";
		
		public static const CMD_DOWN:String = "downLoad";
		public static const CMD_UP:String = "upLoad";
		
		public function HTTP()
		{
			this._loader = new URLLoader();
		}
		
		/**
		 * 
		 * @param vurl url路径
		 * @param dataType 数据类型
		 * @param type 动作类型（上传还是下载）
		 * @param data(表示需要上传的数据)
		 * 
		 */		
		public function load(vurl:String = "",fun:Function = null,dataType:String = HTTP.TEXT,type:String = CMD_UP,data:*=null) : void
		{
			if (vurl == "" && url == ""){
				throw new Error("[HTTP]::url error")
			}
			if (vurl) {
				url = vurl;
			}
			
			if (fun != null) 
			{
				onComplete = fun;
			}
			
			addEvent();
			request = new URLRequest(url);
			
			if(type == CMD_UP){
				request.method = URLRequestMethod.POST;
				_loader.dataFormat = dataType;
				request.data = data;
			}else if(type == CMD_DOWN){
				_loader.dataFormat = dataType;
			}
			_loader.load(this.request);
		}
		
		private function addEvent() : void
		{
			this._loader.addEventListener(Event.COMPLETE, this.complete);
			this._loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.httpStatus);
			this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
			this._loader.addEventListener(ProgressEvent.PROGRESS, this.progressHandler);
		}
		
		private function removeEvent() : void
		{
			this._loader.removeEventListener(Event.COMPLETE, this.complete);
			this._loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.httpStatus);
			this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
			this._loader.removeEventListener(ProgressEvent.PROGRESS, this.progressHandler);
			
		}
		
		private function progressHandler(event:ProgressEvent) : void
		{
			
		}
		
		private function complete(event:Event) : void
		{
			this.removeEvent();
			
			//var obj:Object = JSON.parse(event.target.data);
			if (onComplete is Function){
				onComplete(event.target.data);
			}
		}
		
		private function httpStatus(event:HTTPStatusEvent) : void
		{
			
		}
		
		private function ioErrorHandler(event:IOErrorEvent) : void
		{
			removeEvent();
			if (onError is Function){
				onError();
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent) : void
		{
			removeEvent();
			if (onError is Function){
				onError();
			}
		}
		
		public function get loader() : URLLoader
		{
			return _loader;
		}
		
		public function get request() : URLRequest
		{
			return _request;
		}
		
		public function set request(param1:URLRequest) : void
		{
			this._request = param1;
		}
	}
}
